export function removeEmptyStringProperties(obj) {
  for (let key in obj) {
    if (obj[key] === "") {
      delete obj[key];
    }
  }
  return obj;
}

/*
 * 生成路由
 * @params {Array} allMenuRouteTreeArray 路由列表 [{ path: 路由文件夹 }]
 * @return {Array} 路由列表
 */
export const generateRoutes = (allMenuRouteTreeArray) => {
  const module = import.meta.glob("../views/**/*.vue");

  const menuResult = [];

  // 遍历map
  allMenuRouteTreeArray.map((item) => {
    const url = `../views${item.path}/index.vue`;

    const defComponent = module[url];
    // 跳转uri
    let path = item.menuUri || item.path;

    // 没有配置path, 如果为目录则允许为空， 否则不在加载此配置
    if (!path) {
      if (item.children && item.children.length > 0) {
        path = `/${item.code}`;
        // entId
      } else {
        return; // 不再加载此配置项
      }
    }

    const currentRouter = {
      ...item,
    };

    // 是否有子菜单，并递归处理
    if (item.children && item?.children.length > 0) {
      // Recursion
      currentRouter.children = generateRoutes(item.children);
    } else {
      // 找不到组件 || 其他菜单
      if (!defComponent && item.children) {
        currentRouter.component = module["../views/Main.vue"];
      }
      if (!defComponent) {
        currentRouter.component = module["../views/404/404.vue"];
      }
      currentRouter.component = defComponent;
    }
    menuResult.push(currentRouter);
  });

  return menuResult;
};

/**
 * 判断传入参数是否为空
 * @param {any} arg 要判断的参数
 * @return {Boolean}
 */
export function isEmpty(arg) {
  if (arg == undefined || arg == null) {
    return true;
  }
  if (Object.prototype.toString.call(arg) === "[object Array]") {
    return arg.length == 0;
  }
  if (Object.prototype.toString.call(arg) === "[object Object]") {
    return Object.keys(arg).length == 0;
  }
  let p = arg + "";
  return p.trim().length == 0;
}

export const Storage = {
  set(key, value) {
    if (typeof value === "object") {
      localStorage.setItem(key, JSON.stringify(value));
    }
    if (typeof value === "Array") {
      localStorage.setItem(key, JSON.stringify(value));
    }
    if (typeof value === "string") {
      localStorage.setItem(key, value);
    }
    if (typeof value === "number") {
      localStorage.setItem(key, value.toString());
    }
    if (typeof value === "boolean") {
      localStorage.setItem(key, value ? "true" : "false");
    }
  },
  get(key) {
    const value = localStorage.getItem(key);
    if (value === null) {
      return null;
    }
    try {
      return JSON.parse(value);
    } catch (e) {
      return value;
    }
  },

  remove(key) {
    localStorage.removeItem(key);
  },

  clear() {
    localStorage.clear();
  },
};
